#!/usr/bin/env python3

# Copyright (c) 2020-2021 Fpemud <fpemud@sina.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

import os
import abc
from ._util import StraitObject
from ._game_straits import GameStrait


class MatchContext:

    # it's a decorator
    @staticmethod
    def edit(func):
        def f(self, *kargs, **kwargs):
            assert self._context.edit_or_run
            return func(self, *kargs, **kwargs)
        f._context_edit = True
        return f

    # it's a decorator
    @staticmethod
    def run(func):
        def f(self, *kargs, **kwargs):
            assert not self._context.edit_or_run
            return func(self, *kargs, **kwargs)
        f._context_run = True
        return f

    # it's a decorator
    @staticmethod
    def edit_or_run(func):
        def f(self, *kargs, **kwargs):
            return func(self, *kargs, **kwargs)
        f._context_edit_or_run = True
        return f

    @classmethod
    def match(cls, func, context):
        if context.edit_or_run:
            return hasattr(func, "_context_edit") or hasattr(func, "_context_edit_or_run")
        else:
            return hasattr(func, "_context_run") or hasattr(func, "_context_edit_or_run")


class GameBase(abc.ABC, StraitObject):

    def __init__(self, data_dir):
        StraitObject.__init__(self, GameStrait)

        assert os.path.isdir(data_dir)
        self._dataDir = data_dir

    @property
    @abc.abstractmethod
    def spec(self):
        pass

    @abc.abstractmethod
    def get_title(self, lang):
        pass

    @abc.abstractmethod
    def _prepare(self, edit_or_run, context):
        pass

    @abc.abstractmethod
    def _commit(self):
        pass

    @abc.abstractmethod
    def _cleanup(self, on_error=False):
        pass


class GameSpec:
    pass


class HintForRunner:
    pass


class GameSupportIconDir(abc.ABC):

    def __init__(self, icon_dir):
        self._iconDir = None

    def get_icon_files(self):
        # FIXME
        return {
            "32x32": os.path.join(self._iconDir, "need-for-speed9.png")
        }
